<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:include page="../common/header.jsp">
    <jsp:param name="title" value="商品管理"/>
    <jsp:param name="customCss" value="productManager"/>
</jsp:include>

<div class="container">
    <div class="page-header">
        <h1>商品管理</h1>
        <p class="text-muted">管理商品信息和图片</p>
    </div>

    <div class="product-grid">
        <c:forEach items="${goodsList}" var="goods">
            <div class="product-card">
                <!-- 商品图片部分 -->
                <div class="product-image-container">
                    <img src="${goods.imageData != null && goods.imageType != null ? 'data:image/'.concat(goods.imageType).concat(';base64,').concat(goods.imageData) : pageContext.request.contextPath.concat('/image/noproduct.png')}"
                         alt="${goods.name}"
                         class="product-image">
                    <div class="image-overlay">
                        <form class="upload-form"
                              action="${pageContext.request.contextPath}/admin/productManagerServlet"
                              method="post" enctype="multipart/form-data" style="margin:0;">
                            <input type="hidden" name="action" value="uploadImage">
                            <input type="hidden" name="productId" value="${goods.id}">
                            <label class="upload-button" for="file-${goods.id}">
                                <i class="fas fa-camera"></i>
                                更换图片
                            </label>
                            <input type="file" id="file-${goods.id}" name="image"
                                   accept="image/*" style="display:none;">
                        </form>
                    </div>
                </div>

                <!-- 商品信息编辑部分 -->
                <div class="product-info">
                    <form class="edit-form" action="${pageContext.request.contextPath}/admin/productManagerServlet"
                          method="post">
                        <input type="hidden" name="action" value="updateProduct">
                        <input type="hidden" name="productId" value="${goods.id}">

                        <div class="form-group">
                            <label for="name-${goods.id}">商品名称</label>
                            <input type="text" class="form-control" id="name-${goods.id}"
                                   name="name" value="${goods.name}" required>
                        </div>

                        <div class="form-group">
                            <label for="price-${goods.id}">价格</label>
                            <div class="input-group">
                                <span class="input-group-text">¥</span>
                                <input type="number" class="form-control" id="price-${goods.id}"
                                       name="price" value="${goods.price}" step="0.01" min="0" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="stock-${goods.id}">库存</label>
                            <input type="number" class="form-control" id="stock-${goods.id}"
                                   name="stock" value="${goods.stock}" min="0" required>
                        </div>

                        <div class="form-group">
                            <label for="description-${goods.id}">商品描述</label>
                            <textarea class="form-control" id="description-${goods.id}"
                                      name="description" rows="3">${goods.description}</textarea>
                        </div>

                        <div class="btn-group mt-3">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> 保存修改
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </c:forEach>
    </div>
</div>

<!-- 操作提示框 -->
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast-header">
        <strong class="mr-auto">提示</strong>
        <button type="button" class="ml-2 mb-1 close" data-dismiss="toast">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="toast-body"></div>
</div>

<jsp:include page="../common/footer.jsp">
    <jsp:param name="customJs" value="productManager"/>
</jsp:include>
